package com.frinder.official.akka.routing;

import akka.actor.ActorRef;
import akka.routing.*;
import com.frinder.official.spring.MyBaseActor;
import com.google.common.collect.Lists;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import scala.concurrent.duration.FiniteDuration;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by frinder on 2017/9/29.
 */
@Component
@Scope("prototype")
public class GroupRouter extends MyBaseActor {

    public void group() {

        // create router by configuration(routing.conf)
        ActorRef router3 =
                getContext().actorOf(FromConfig.getInstance().props(), "router3");

        // create router by programmatically
        List<String> paths = Lists.newArrayList("/user/children/c1", "/user/children/c2", "/user/children/c3");
        ActorRef router4 =
                getContext().actorOf(new RoundRobinGroup(paths).props(), "router4");

        // remote group
        ActorRef routerRemoteGroup =
                getContext().actorOf(FromConfig.getInstance().props(), "remoteGroup");

        // RandomGroup defined in configuration:
        ActorRef router7 =
                getContext().actorOf(FromConfig.getInstance().props(), "router7");

        // RandomGroup defined in code:
        ActorRef router8 =
                getContext().actorOf(new RandomGroup(paths).props(), "router8");


        // BroadcastGroup defined in configuration:
        ActorRef router15 =
                getContext().actorOf(FromConfig.getInstance().props(), "router15");

        // BroadcastGroup defined in code:
        ActorRef router16 =
                getContext().actorOf(new BroadcastGroup(paths).props(), "router16");

        // ScatterGatherFirstCompletedGroup defined in configuration:
        ActorRef router19 =
                getContext().actorOf(FromConfig.getInstance().props(), "router19");

        // ScatterGatherFirstCompletedGroup defined in code:
        FiniteDuration within2 = FiniteDuration.create(10, TimeUnit.SECONDS);
        ActorRef router20 =
                getContext().actorOf(
                        new ScatterGatherFirstCompletedGroup(paths, within2).props(),
                        "router20");

        // TailChoppingGroup defined in configuration:
        ActorRef router23 =
                getContext().actorOf(FromConfig.getInstance().props(), "router23");

        // TailChoppingGroup defined in code:
        FiniteDuration within4 = FiniteDuration.create(10, TimeUnit.SECONDS);
        FiniteDuration interval2 = FiniteDuration.create(20, TimeUnit.MILLISECONDS);
        ActorRef router24 =
                getContext().actorOf(new TailChoppingGroup(paths, within4, interval2).props(),
                        "router24");

        // ConsistentHashingGroup defined in configuration:
        ActorRef router27 =
                getContext().actorOf(FromConfig.getInstance().props(), "router27");

        // ConsistentHashingGroup defined in code:
        ActorRef router28 =
                getContext().actorOf(new ConsistentHashingGroup(paths).props(), "router28");
    }

    @Override
    public Receive createReceive() {
        return null;
    }
}
